package pl.kane.autokomp.applications;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import pl.kane.autokomp.applications.exceptions.SystemException;



public class MainFrame 
implements ActionListener, MouseListener, MouseMotionListener{
	
	
	private static MainFrame instance = null;
	static private JFrame frame;
	static StringBuffer consoleText=new StringBuffer();
	static StringBuffer codeText=new StringBuffer();
	static private JTextArea console;
	static private JTextArea code;
	static String logger;
	
	String currentFileName;
	
	
	
	public MainFrame(){
		super();
	}
		
	public static MainFrame getInstance() {
		if (instance==null)
			return new MainFrame();
		else
			return instance;
	}
	

	
	 private static void createAndShowGUI(MainFrame ak) {
	        //Make sure we have nice window decorations.
	        JFrame.setDefaultLookAndFeelDecorated(true);
	     
	        //Create and set up the window.
	        frame = new JFrame("AutoKomp 2.0");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        frame.addMouseListener(ak);
	        frame.addMouseMotionListener(ak);
	        frame.getContentPane().addMouseListener(ak);
	        frame.getContentPane().addMouseMotionListener(ak);
	        
	        try {
				frame.setJMenuBar(new MainMenu());
			} catch (SystemException e) {
				e.printStackTrace();
			}
	        frame.getContentPane().add(new ToolBar(), BorderLayout.NORTH);
	        JPanel upperPanel = new JPanel();
	        JPanel lowerPanel = new JPanel();
	        frame.getContentPane().add(upperPanel, "Center");
	        frame.getContentPane().add(lowerPanel, "South");

	        console = new JTextArea(consoleText.toString(), 10, 80);
	        console.setEditable(false);
	        addToConsole("Welcome to AutoKomp 2.0\n");
	        lowerPanel.add(new JScrollPane(console));
	        code = new JTextArea(codeText.toString(), 30, 80);
	        code.setLineWrap(true);
	        upperPanel.add(new JScrollPane(code));
	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	        
	    }

	public static void addToConsole(String text) {
		consoleText.append(text);
		console.setText(consoleText.toString());
		console.repaint();
	}
	public static void clearConsole() {
		consoleText=new StringBuffer();
		console.setText(consoleText.toString());
		console.repaint();
	}
	
	public static void setCode(String text) {
		codeText=new StringBuffer();
		codeText.append(text);
		code.setText(codeText.toString());
		code.repaint();
	}
	public static String getCode() {
		return code.getText();
	}
	public static void clearCode() {
		codeText=new StringBuffer();
		code.setText(codeText.toString());
		code.repaint();
	}
	public static void hideWindow() {
		frame.hide();
	}
	
	public static void showWindow() {
		frame.show();
	}
	
	
	
	public static void main(String [] args) {
		final MainFrame instance = new MainFrame();
		
		
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	
                createAndShowGUI(instance);
                
            }
        });
	
	}
	
	public void actionPerformed(ActionEvent e) {
		
	}
	public void mouseClicked(MouseEvent e) {
//		 TODO Auto-generated method stub
		
	}
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseMoved(MouseEvent e) {
//		 TODO Auto-generated method stub
	}

	public static JFrame getFrame() {
		return frame;
	}

	public String getCurrentFileName() {
		return currentFileName;
	}

	public void setCurrentFileName(String currentFileName) {
		this.currentFileName = currentFileName;
	}
}
